<template>
  <el-dialog title="编辑模板" :visible.sync="FormVisible" width="400px">
    <el-form :model="form" label-width="100px" :rules="rules" ref="ruleForm">
      <el-form-item label="模板标题：" prop="mailTemplateName">
        <el-input v-model="form.mailTemplateName" placeholder="请输入标题"></el-input>
      </el-form-item>
      <el-form-item label="邮件内容：" prop="context">
        <el-input
          type="textarea"
          :autosize="{ minRows: 4, maxRows: 6}"
          placeholder="请输入内容"
          v-model="form.context"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="showModule">取 消</el-button>
      <el-button @click="_addMailTemplate('ruleForm')">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
const ERR_OK = 200;
export default {
  data() {
    return {
      FormVisible: false,
      form: {},
      rules: {
        mailTemplateName: [
          { required: true, message: "请输入模板标题", trigger: "blur" }
        ],
        context: [
          { required: true, message: "请输入模板内容", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    showModule(item) {
      this.form = JSON.parse(JSON.stringify(item))
      this.FormVisible = this.FormVisible ? false : true;
    },
    // 编辑模板
    _addMailTemplate(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$ajax({
            method: "post",
            url:  this.GLOBAL.warrantyService + "/mailTemplate/updateMailTemplate",
            data: this.form
          }).then(res => {
            if (res.data.code === ERR_OK) {
              this.FormVisible = false;
              this.$message.success("编辑成功");
              this.$emit('edit')
            } else {
              this.$message.error("编辑失败");
            }
          });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

